package cn.tanglong.quartz.job;

import cn.tanglong.order.domain.Order;
import cn.tanglong.order.domain.OrderPetDetail;
import cn.tanglong.order.mapper.OrderMapper;
import cn.tanglong.order.mapper.OrderPetDetailMapper;
import cn.tanglong.pay.dimain.PayBill;
import cn.tanglong.pay.mapper.PayBillMapper;
import cn.tanglong.pet.domain.Pet;
import cn.tanglong.pet.mapper.PetMapper;
import cn.tanglong.quartz.util.QuartzUtil;
import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.payment.common.models.AlipayTradeCloseResponse;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//@Component
public class OrderExpireJob implements Job {

    @Autowired
    private PayBillMapper payBillMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private OrderPetDetailMapper orderPetDetailMapper;

    @Autowired
    private Scheduler scheduler;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        // 获取数据 拿到一个map
        JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
        //数据
        //TODO 报错了这里 说string不能转map
        Map<String,String> map = (Map<String, String>) jobDataMap.get("params");
        String orderNu = map.get("orderNo");

        // 查询支付订单号
        PayBill payBill = payBillMapper.selectLoadBySn(orderNu);
        String jobName = map.get("jobName");

        // 判断状态，不是待支付 直接掐断
        if(payBill.getState().intValue() != PayBill.SATET_WAIT_PAY){
            QuartzUtil.removeJob(scheduler, jobName);
            System.out.println("定时任务删掉了哦");
            return;
        }

        // 查询业务单
        Order order = orderMapper.selectByOrderSn(orderNu);
        if (order.getState().intValue() != Order.STATE_WAIT_PAY){
            QuartzUtil.removeJob(scheduler, jobName);
            System.out.println("定时任务删掉了哦");
            return;
        }

        // 是待支付，开始处理
        // 修改状态为超时 时间改为当前时间
        payBill.setState(PayBill.SATET_CANCEL);
        Date date = new Date();
        payBill.setUpdateTime(date);
        payBillMapper.update(payBill);

        // 业务单，改为超时
        order.setState(Order.STATE_PAY_OUTTIME_CANCEL);
        order.setLastUpdateTime(date);
        orderMapper.update(order);

        // 查询宠物状态
        List<OrderPetDetail> details = orderPetDetailMapper.selectByOrderNo(orderNu);

        // 拿到宠物id的集合
        List<Long> collect = details.stream().map(detail -> detail.getPetId()).collect(Collectors.toList());

        // 批量查询宠物
        List<Pet> pets = petMapper.batchSelectByIds(collect, Pet.STATE_ONLINE);

        // 批量修改
        pets.forEach(pet ->{
            // 应该是加detail中的数量， 但是我们只买一个 所以就+1
            pet.setStore_number(pet.getStore_number()+1);
            petMapper.update(pet);
        });

        // 调用支付宝的关闭接口 关闭订单
        try {
            AlipayTradeCloseResponse close = Factory.Payment.Common().close(orderNu);
            System.out.println(close);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 删除定时任务
        QuartzUtil.removeJob(scheduler, jobName);
        System.out.println("定时任务删掉了哦");
    }
}
